﻿Public Class perUsuario
    Inherits perPadrao

#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
    Private IDUsuario As Int32
#End Region

#Region "Propriedades"

    Public ReadOnly Property sqlConsulta() As String
        Get
            Dim sSql As String
            sSql = "SELECT IDUsuario, Nome " & vbCrLf
            sSql &= "FROM DC_Usuarios" & vbCrLf
            sSql &= "WHERE IDUsuario > 0" & vbCrLf
            sSql &= "ORDER BY Nome"
            Return sSql
        End Get
    End Property
    Public Property Usuario()
        Get
            Return IDUsuario
        End Get
        Set(ByVal value)
            IDUsuario = value
        End Set
    End Property

#End Region

#Region "Métodos"

    Public Function inserirUsuario(ByVal sNome As String, _
                                   ByVal sLogin As String, _
                                   ByVal sSenha As String, _
                                   ByVal iIDGrupoAcesso As Int32) As Int32

        Dim iIdUsuario As Int32
        Dim sSql As String

        Try
            iIdUsuario = Me.objBuscaId.BuscaID("IDUsuario", "DC_Usuarios")

            sSql = "  INSERT INTO DC_Usuarios "
            sSql &= " ( "
            sSql &= "   IDUsuario, "
            sSql &= "   Nome, "
            sSql &= "   Login, "
            sSql &= "   Senha, "
            sSql &= "   IDGrupoAcesso"
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= " ( "
            sSql &= iIdUsuario & ", "
            sSql &= "'" & sNome & "', "
            sSql &= "'" & sLogin & "', "
            sSql &= "'" & sSenha & "', "
            sSql &= iIDGrupoAcesso
            sSql &= " ) "

            MyBase.executarAcao(sSql)

            Return iIdUsuario
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirUsuario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarUsuario(ByVal iIdUsuario As Int32, _
                              ByVal sNome As String, _
                              ByVal sLogin As String, _
                              ByVal sSenha As String, _
                              ByVal iIDGrupoAcesso As Int32)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_Usuarios SET "
            sSql &= "   Nome = '" & sNome & "', "
            sSql &= "   Login = '" & sLogin & "', "
            sSql &= "   Senha = '" & sSenha & "', "
            sSql &= "   IDGrupoAcesso = '" & iIDGrupoAcesso & "'"
            sSql &= " WHERE "
            sSql &= "   IdUsuario = " & iIdUsuario

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarUsuario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirUsuario(ByVal iIdUsuario As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_Usuarios "
            sSql &= " WHERE "
            sSql &= "   IDUsuario = " & iIdUsuario

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirUsuario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarCampoUsuario(ByVal sCampo As String, _
                                           ByVal iIDUsuario As Long) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT " & sCampo
            sSql &= " FROM DC_Usuarios "

            If (iIDUsuario > 0) Then
                sSql &= " WHERE IDUsuario = " & iIDUsuario
            End If

            dsDados = MyBase.executarConsulta(sSql, "DC_Usuario")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Usuario").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Usuario")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCampoUsuario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarUsuario(ByVal iIdUsuario As Int32) As DataRow
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_Usuarios "
            sSql &= " WHERE IDUsuario = " & iIdUsuario

            dsDados = MyBase.executarConsulta(sSql, "DC_Usuario")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Usuario").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Usuario").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarUsuario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
    Public Function verificarUsuario(ByVal sLogin As String) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT *"
            sSql &= " FROM DC_Usuarios "
            sSql &= " WHERE Login = '" & sLogin & "'"

            dsDados = MyBase.executarConsulta(sSql, "DC_Usuarios")

            Return dsDados.Tables("DC_Usuarios")

        Catch ex As Exception
            Throw New Exception("Exceção no método 'verificarUsuario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
#End Region
End Class
